package com.niit.reflect;

import com.niit.beans.User;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author hanli
 * @date 2025/3/4
 * @description TODO
 */
public class TestReflect {
    public static void main(String[] args) {

        try {
            // clazz1 public class  User.java User定义
            Class clazz1 = Class.forName("com.niit.beans.User");

            Class clazz2 = User.class;

            // user1 相当于 new 出来User的实例
            Object user1 = clazz1.getDeclaredConstructor().newInstance();

            Method method = clazz1.getMethod("say");
            method.invoke(user1);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }
}